package util

import (
	"go-yuque/logger"
	"io"
	"net/http"
	"time"
)

type FakeBrowserStruct struct {
	Url     string
	Headers http.Header
	Body    map[string]string
}

func (f *FakeBrowserStruct) DoGet() ([]byte, error) {
	client := http.Client{
		Timeout: 30 * time.Second,
	}

	req, err := http.NewRequest("GET", f.Url, nil)
	if err != nil {
		logger.Errorf("生成请求报错,Url:[%s],err:%v", f.Url, err)
		return nil, err
	}

	req.Header = f.Headers

	resp, err := client.Do(req)
	if err != nil {
		logger.Errorf("请求报错,Url:[%s],err:%v", f.Url, err)
		return nil, err
	}

	defer func(Body io.ReadCloser) {
		err := Body.Close()
		if err != nil {

		}
	}(resp.Body)
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		logger.Errorf("读取body报错,Url:[%s],err:%v", f.Url, err)
		return nil, err
	}
	logger.Infof("请求成功,Url:[%s]", f.Url)
	return body, nil
}
